package org.onion.pattern.event.idempotent;


import org.onion.pattern.event.exception.EventConsumeIdempotentException;

import java.util.function.Supplier;

/**
 * 消费幂等验证
 */
@FunctionalInterface
public interface EventIdempotentService {

    IdempotentStatus mark(String handlerName, String eventType, String eventKey);

    /**
     * 验证幂等结果
     *
     * @param handlerName 处理器名称
     * @param eventKey    事件key
     */
    default void execute(String handlerName, String eventType, String eventKey, Supplier<?> handle) throws EventConsumeIdempotentException {
        IdempotentStatus mark = this.mark(handlerName, eventType, eventKey);
        switch (mark) {
            case UNCONSUMED:
                break;
            default:
                throw new EventConsumeIdempotentException(eventKey + "在处理 " + handlerName + " 已经处理了");
        }
        handle.get();
    }

}
